import { createSlice } from "@reduxjs/toolkit";

const tabSlice = createSlice({
    name: "tab",
    initialState: {
        isCollapse:false,
        // 面包屑的默认数据
        tabList:[
            {
                path:'/',
                name:'home',
                label:'首页'
            }
        ],
        //得到当前选中的数据
        currentMenu:{},
        //当前的菜单路由
        menuList:[]
    },
    reducers: {
        collapseMenu: (state) => {
            state.isCollapse =!state.isCollapse
        },
        selectMenuList:(state,{payload:value})=>{
            if(value.name !=='home'){
                state.currentMenu = value
                //判断是否有重复的
                const result = state.tabList.findIndex(item => item.name === value.name) 
                //判断传进来的值是否有重复的，没有findIndex返回-1

                //如果不是首页
                if(result === -1){
                    state.tabList.push(value)
                }
               
            }else if(value.name === 'home' && state.tabList.length === 1){
                //如果是首页,并且仅仅只有首页就清空
                state.currentMenu = {}

            }

        },
        //清空面包屑的数据
        clearTabList:(state)=>{
            state.tabList = [
                {
                    path:'/',
                    name:'home',
                    label:'首页'
                }
            ]
        },
        //关闭的逻辑
        closeTag(state,{payload:value}){
            const result = state.tabList.findIndex(item => item.name === value.name)
            state.tabList.splice(result,1)
        },
        //设置当前选中的标签
        setMenuList: (state,{payload:value})=>{
            if(value.name === 'home'){
                state.currentMenu = {}
            }else{
                state.currentMenu = value
            }
        },
        //得到当前的路由菜单
        getMenuList:(state,{payload:value})=>{
            state.menuList = value
        }

    }
})

export const { collapseMenu,selectMenuList,closeTag,setMenuList,getMenuList,clearTabList } = tabSlice.actions;
export default tabSlice.reducer;